/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */
#include "camera.h"

//! The Camera object is only one for each node but can change type or FOV and other parameters.
Camera::Camera(IrrlichtDevice *device)
  {
    //ctor
    Device = device;

    ICamera[0] = Device->getSceneManager()->addCameraSceneNode();
    ICamera[1] = Device->getSceneManager()->addCameraSceneNodeFPS(0, 100.0f, 0);
    ICamera[1]->setID(-32); // for Irrlicht 1.5
    ICamera[2] = Device->getSceneManager()->addCameraSceneNodeMaya(0, -1000.0f,0,0);
    Device->getSceneManager()->setActiveCamera(ICamera[0]);
    setType(ECT_FIXED);
  }
Camera::~Camera()
  {
    //dtor
  }

// Set a type suitable for the situation.
// \param type use FIXED for simple slide, FPS for pano nodes, MAYA for edit (at the moment).
void Camera::setType(ECAMERA_TYPE type)
  {
    Type = type;
    vector3df target = Device->getSceneManager()->getActiveCamera()->getTarget();
    f32 fov = Device->getSceneManager()->getActiveCamera()->getFOV();
    switch (type)
      {
    case ECT_FIXED:
      Device->getSceneManager()->setActiveCamera(ICamera[0]);
      Device->getSceneManager()->getActiveCamera()->setAspectRatio(1.0f);
      Device->getSceneManager()->getActiveCamera()->setFOV(PI/2);
      break;
    case ECT_FPS:
      ICamera[2]->setInputReceiverEnabled(false);
      ICamera[1]->setInputReceiverEnabled(true);
      Device->getSceneManager()->setActiveCamera(ICamera[1]);
      //Device->getSceneManager()->getActiveCamera()->setTarget(target);
      //Device->getSceneManager()->getActiveCamera()->setFOV(fov);
      break;
    case ECT_MAYA:
      ICamera[1]->setInputReceiverEnabled(false);
      ICamera[2]->setInputReceiverEnabled(true);
      Device->getSceneManager()->setActiveCamera(ICamera[2]);
      //Device->getSceneManager()->getActiveCamera()->setTarget(target);
      //Device->getSceneManager()->getActiveCamera()->setFOV(fov);
      break;
      }
  }

//! \return type of the current camera.
ECAMERA_TYPE Camera::getType()
  {
    return Type;
  }

//Destroy the current camera and create a new one with the new speed
// \param newSpeed 100.0f is default for FPS; -1500.0f is default for MAYA.
void Camera::setSpeed(float newSpeed)
  {
    vector3df target = getViewDirection();
    f32 fov = getFov();
    switch (Type)
      {
    case ECT_FIXED:
      /* do nothing */
      break;
    case ECT_FPS:
      Device->getSceneManager()->getActiveCamera()->drop();
      Device->getSceneManager()->addCameraSceneNodeFPS(0, 100.0f * newSpeed, 0);
      break;
    case ECT_MAYA:
      Device->getSceneManager()->getActiveCamera()->drop();
      Device->getSceneManager()->addCameraSceneNodeMaya(0, -1500.0f * newSpeed, 0);

      break;
      }
    Device->getSceneManager()->getActiveCamera()->setAspectRatio(1.0f);
    Device->getSceneManager()->getActiveCamera()->setTarget(target);
    Device->getSceneManager()->getActiveCamera()->setFOV(fov);
  }

// \return speed of the camera.
float Camera::getSpeed()
  {
    return Speed;
  }
// \param fov fov in degrees of the camera.
void Camera::setFov(float fov)
  {
    Device->getSceneManager()->getActiveCamera()->setFOV(fov * DEGTORAD);
  }

// \return fov in degrees of the camera.
float Camera::getFov()
  {
    return Device->getSceneManager()->getActiveCamera()->getFOV() * RADTODEG;
  }

// \param direction vector 3d of the view diretion.
void Camera::setViewDirection(vector3df direction)
  {
    Device->getSceneManager()->getActiveCamera()->setTarget(direction);
  }

// \return vector 3d of the current view direction.
vector3df Camera::getViewDirection()
  {
    return Device->getSceneManager()->getActiveCamera()->getTarget();
  }
